package com.thinkwide.data.ormapping.sql;

import com.thinkwide.data.ormapping.exceptions.MFSqlParamException;

import java.util.Date;
import java.util.Objects;

public class MFSqlBase {
    public static final String PARAM = " ? ";
    public static final String WHERE = " WHERE ";
    public static final String ORDER_BY = " Order By ";
    public static final String SELECT = " Select ";
    public static final String cAsterisk = " * ";
    public static final String DESC = " DESC ";

    public static final String ASC = " ASC ";
    public static final String AS = " AS ";
    public static final String DELETE = " DELETE ";
    public static final String UPDATE = " UPDATE ";
    public static final String SET = " SET ";
    public static final String AND = " AND ";
    public static final String OR = " OR ";
    public static final String NOT = " NOT ";

    public static final String DISTINCT = " DISTINCT ";
    public static final String GROUP_BY = " GROUP BY ";
    public static final String HAVING = " HAVING ";
    public static final String UNION = " UNION ";
    public static final String ALL = " ALL ";
    public static final String WITH = " WITH ";
    public static final String INTO = " INTO ";
    public static final String AVG = " AVG ";
    public static final String CAST = " CAST";
    public static final String COUNT = " COUNT";
    public static final String MAX = " MAX";
    public static final String MIN = " MIN";
    public static final String SUM = " SUM";
    public static final String VALUES = " VALUES";

    public static final String EQUAL = "=";
    public static final String LIKE = "LIKE";
    public static final String IN = "IN";
    public static final String VIRGULE = "/";
    public static final String LESS = "<";
    public static final String MORE = ">";
    public static final String QUOTATION = "\"";
    public static final String SPACE = " ";
    public static final String COMMA = ",";
    public static final String LBRACKET = "(";
    public static final String RBRACKET = ")";


    public static String replace(String strValue, String replaceValue, Object[] args) {
        int i = 0;
        int j = 0;
        String strResult = "";
        while (i < strValue.length()) {
            if (replaceValue.length() <= (strValue.length() - i)) {
                String str = strValue.substring(i, replaceValue.length() + i);
                if (replaceValue.equals(str)) {
                    strResult += args[j].toString();
                    i += replaceValue.length();
                    j++;
                    continue;
                }

            }
            strResult += strValue.charAt(i);
            i++;
        }

        return strResult;
    }

    public static String getStaticSql(String sqlString, Object... args) {
        int i = 0;
        if (args == null) {
            return sqlString;
        }

        for (Object item : args) {
            if (Objects.isNull(item)) {
                throw new MFSqlParamException(1001, "SQL参数不能为空");
            }

            if (item instanceof String || item instanceof Date) {
                args[i] = String.format("'%s'", item);
            }

            //sqlString = sqlString.Replace("{" + i.ToString() + "}", args[i].ToString());

            i++;
        }

        sqlString = MFSqlBase.replace(sqlString, "?", args);
        return sqlString;
    }
}
